package edu.ccut.saturn.studio.component.wizard;

import java.io.File;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.internal.ui.dialogs.TypeSelectionDialog2;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.component.dialog.CreateClassDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.wizard.SaturnWizardPage;

public class ComponentWizardPage extends SaturnWizardPage {

	private static final String SATURN_COMPONTENTFILEWZARD_COMPONTENT_PROJECTNAME = "componentProjectName"; //$NON-NLS-1$
	private static final String SATURN_COMPONTENTFILEWZARD_COMPONTENT_DESCRIPTION = "componentDescription"; //$NON-NLS-1$
	private static final String SATURN_COMPONTENTFILEWZARD_COMPONTENT_NAME = "componentName"; //$NON-NLS-1$
	public static final String SATURN_COMPONTENTFILEWZARD_COMPONTENT_FILENAME = "componentFileName"; //$NON-NLS-1$
	private static final String SATURN_COMPONTENTFILEWZARD_COMPONTENT_KEY = "componentKey"; //$NON-NLS-1$
	private static final String SATURN_COMPONTENTFILEWZARD_COMPONTENT_CLASS = "componentClass"; //$NON-NLS-1$

	private IProject project;
	private String projectFilePath;// 文件的包名

//	private IStructuredSelection selection;

	private Text txtKeyField;
	private Text txtComponentName;
	private Text txtDescription;
	private Text txtClass;
	private Text txtFileName;

	public ComponentWizardPage(IStructuredSelection selection) {
		super("create saturn data"); //$NON-NLS-1$
		setTitle(Messages.getString("ComponentWizardPage.installparameter")); //$NON-NLS-1$
		setDescription(Messages
				.getString("ComponentWizardPage.installbasicmessage")); //$NON-NLS-1$
//		this.selection = selection;

		this.project = SaturnProjectUtil.getProject(selection);
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.marginRight = 5;
		gridLayout.marginLeft = 5;
		gridLayout.numColumns = 4;
		container.setLayout(gridLayout);
		setControl(container);

		final Label className = new Label(container, SWT.NONE);
		className.setLayoutData(new GridData());
		className.setText(Messages
				.getString("ComponentWizardPage.componentclass")); //$NON-NLS-1$

		txtClass = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_textClass = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		gd_textClass.widthHint = 349;
		txtClass.setLayoutData(gd_textClass);
		txtClass.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		final Button butBrowse = new Button(container, SWT.NONE);
		final GridData gd_butBrowse = new GridData(50, SWT.DEFAULT);
		butBrowse.setLayoutData(gd_butBrowse);
		butBrowse.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				browseForDestinationFile();
			}
		});
		butBrowse.setText(Messages.getString("ComponentWizardPage.browsing")); //$NON-NLS-1$

		final Button createClassButton = new Button(container, SWT.NONE);
		createClassButton.setLayoutData(new GridData(50, SWT.DEFAULT));
		createClassButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				// CreateClassDialog classDialog = new CreateClassDialog(shell,
				// project, srcName, projectFilePath);
				// classDialog.setSelection(selection);
				projectFilePath = getPackageName();
				CreateClassDialog classDialog = new CreateClassDialog(
						getShell(), getProject(), projectFilePath);

				if (classDialog.open() == IDialogConstants.OK_ID) {
					txtClass.setText(classDialog.getPkgName()
							+ "." + classDialog.getClassName()); //$NON-NLS-1$
				}
			}
		});
		createClassButton.setText(Messages
				.getString("ComponentWizardPage.create")); //$NON-NLS-1$

		final Label keyLabel = new Label(container, SWT.NONE);
		final GridData gd_keyLabel = new GridData(60, SWT.DEFAULT);
		keyLabel.setLayoutData(gd_keyLabel);
		keyLabel.setText(Messages.getString("ComponentWizardPage.keyvalue")); //$NON-NLS-1$

		GridData gd = new GridData();

		txtKeyField = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
		txtKeyField.setLayoutData(gd);

		txtKeyField.setText(getComponentKey("")); //$NON-NLS-1$

		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label fileNamelabel = new Label(container, SWT.NONE);
		fileNamelabel.setText(Messages
				.getString("ComponentWizardPage.filename")); //$NON-NLS-1$

		txtFileName = new Text(container, SWT.BORDER);
		txtFileName.setTextLimit(50);
		gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
		txtFileName.setLayoutData(gd);
		txtFileName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				txtKeyField.setText(getComponentKey(txtFileName.getText()
						+ ".component")); //$NON-NLS-1$
				validate();
			}
		});
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label nameLabel = new Label(container, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("ComponentWizardPage.name")); //$NON-NLS-1$

		txtComponentName = new Text(container, SWT.BORDER);
		txtComponentName.setTextLimit(50);
		gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
		txtComponentName.setLayoutData(gd);
		txtComponentName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				txtComponentName.getText();
				validate();
			}
		});

		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		final Label descriptionLabel = new Label(container, SWT.NONE);
		descriptionLabel.setText(Messages
				.getString("ComponentWizardPage.descirption")); //$NON-NLS-1$
		descriptionLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false,
				true));

		txtDescription = new Text(container, SWT.V_SCROLL | SWT.H_SCROLL
				| SWT.BORDER | SWT.WRAP);
		txtDescription.setTextLimit(200);
		gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd.heightHint = 72;
		gd.widthHint = 343;
		txtDescription.setLayoutData(gd);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		validate();

	}

	protected void browseForDestinationFile() {
		IJavaSearchScope searchScope = SearchEngine.createWorkspaceScope();
		TypeSelectionDialog2 dialog = new TypeSelectionDialog2(null, false,
				new ProgressMonitorDialog(null), searchScope,
				IJavaSearchConstants.TYPE);
		dialog.setMessage(Messages
				.getString("SelectFilesWizardPage.pleasechooseclass")); //$NON-NLS-1$
		dialog.setBlockOnOpen(true);
		dialog
				.setTitle(Messages
						.getString("SelectFilesWizardPage.chooseclass")); //$NON-NLS-1$
		if (Dialog.OK == dialog.open()) {
			IType obj = (IType) dialog.getFirstResult();
			txtClass.setText(obj.getFullyQualifiedName());
		}
	}

	// 初始化map值
	public void initalValues(Map<String, Object> values) {
		values.put(SATURN_COMPONTENTFILEWZARD_COMPONTENT_CLASS, this.txtClass
				.getText().trim());
		values.put(SATURN_COMPONTENTFILEWZARD_COMPONTENT_KEY, this.txtKeyField
				.getText().trim());
		values.put(SATURN_COMPONTENTFILEWZARD_COMPONTENT_FILENAME,
				this.txtFileName.getText().trim());
		values.put(SATURN_COMPONTENTFILEWZARD_COMPONTENT_NAME,
				this.txtComponentName.getText().trim());
		values.put(SATURN_COMPONTENTFILEWZARD_COMPONTENT_DESCRIPTION,
				this.txtDescription.getText().trim());
		values.put(SATURN_COMPONTENTFILEWZARD_COMPONTENT_PROJECTNAME, project
				.getName());
	}

	// 添加验证方法
	public boolean validate() {
		String fileName = txtFileName.getText().trim();
		String componentName = txtComponentName.getText().trim();
		String componentClass = txtClass.getText().trim();

		setPageComplete(false);

		if (SaturnValidation.isEmpty(componentClass)) {
			setErrorMessage((Messages
					.getString("ComponentWizardPage.componentclasscannotempty"))); //$NON-NLS-1$
			return false;
		}

		if (new File(getFullPath() + "/" + fileName + ".component").exists()) {
			setErrorMessage(Messages.getString("ComponentWizardPage.FileNameExist")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(fileName)) {
			setErrorMessage(Messages.getString("ComponentWizardPage.FileNameEmpty")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isBadWord(fileName)) {
			setErrorMessage(Messages.getString("ComponentWizardPage.FileNameBad")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isEmpty(componentName)) {
			setErrorMessage(Messages.getString("ComponentWizardPage.NameEmpty")); //$NON-NLS-1$
			return false;
		}

		if (SaturnValidation.isBadWord(componentName)) {
			setErrorMessage(Messages.getString("ComponentWizardPage.NameBad")); //$NON-NLS-1$
			return false;
		}

		setErrorMessage(null);
		setPageComplete(true);
		return true;

		// if (SaturnValidation.isEmpty(this.txtFileName.getText().trim())) {
		// setErrorMessage((Messages
		// .getString("ComponentWizardPage.filenamecannotempty")));
		// //$NON-NLS-1$
		// } else {
		// if (SaturnValidation.isGB2312(this.txtFileName.getText().trim())) {
		// setErrorMessage((Messages
		// .getString("ComponentWizardPage.filenamecannotbechinese")));
		// //$NON-NLS-1$
		// } else {
		// if (SaturnValidation.isEmpty(this.txtComponentName.getText()
		// .trim())) {
		// setErrorMessage((Messages
		// .getString("ComponentWizardPage.namecannotempty"))); //$NON-NLS-1$
		//
		// } else {
		// setErrorMessage(null);
		// setPageComplete(true);
		// return true;
		// }
		// }
		// }
		//
		// return false;
	}

	private IProject getProject() {
		if (project == null) {
			this.project = SaturnProjectUtil.getProject(super.getSelection());
		}

		return this.project;
	}
}
